<?php
/**
 * TomatoCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@tomatocms.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2009-2010 TIG Corporation (http://www.tig.vn)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @version 	$Id: Product.php 5341 2010-09-07 09:04:47Z huuphuoc $
 * @since		2.0.5
 */

class Product_Models_Dao_Pdo_Mysql_Product extends Tomato_Model_Dao
	implements Product_Models_Interface_Product
{
	public function convert($entity) 
	{
		return new Product_Models_Product($entity); 
	}
	// Tìm mã số tiếp theo của sản phẩm thêm mới vào
	public function getNextId(){		
		$config 					= Tomato_Config::getConfig(); 
		$prefix_code_product		= $config->get('prefix_code')->product_code;
		$prefix_code_product_length = strlen($prefix_code_product);
		$name_ini_length			= 'product_code_length';
		$length_max_id				= $config->get('prefix_code')->$name_ini_length;
			
		$select = $this->_conn
					->select()
					->from(array('p' => $this->_prefix . 'product'),'MAX(SUBSTR(p.id,'.($prefix_code_product_length+1).')) as MAX_ID');
		$row = $select->query()
			   		  ->fetch();	
		$max_id =  (null == $row) ? 0 : $row->MAX_ID;
		$max_id++;
		$addition_zero_num 		= $length_max_id - strlen($max_id) - strlen($prefix_code_product);
		
		$code = $prefix_code_product;
		for($i=1;$i<=$addition_zero_num;$i++)
			$code.='0';
		$code.= $max_id;
		return $code;	
	}
	
	public function find($offset,$count,$exp=null){
		$select = $this->_conn
					->select()
					->from(array('c' => $this->_prefix . 'product'));
		if ($exp) {
            if (isset($exp['shop_id'])) {
				$select->where("c.shop_id=?",$exp['shop_id']);
			}
		}		
        $select->order('c.id DESC');
        if(null != $count){
            $select->limit($count, $offset);
        }
        $rs = $select->query()
                     ->fetchAll();
        return new Tomato_Model_RecordSet($rs, $this);		
	}
	
	
	public 	function count($exp){
		$select = $this->_conn
                        ->select()
                        ->from(array('c' => $this->_prefix . 'product'), array('num_product' => 'COUNT(*)'));
     	if ($exp) {
            if (isset($exp['shop_id'])) {
				$select->where("c.shop_id=?",$exp['shop_id']);
			}
		}		
        return $select->query()->fetch()->num_product;	
	}	
	
	public function getById($id) 
	{
		$row = $this->_conn
					->select()
					->from(array('c' => $this->_prefix . 'product'))
					->where('c.id = ?', $id)
					->limit(1)
					->query()
					->fetch();
		return (null == $row) ? null : new Product_Models_Product($row);
	}

	public function add($product) 
	{
			$data = array(
				'id'				=>	$product->id,
				'shop_id'			=>  $product->shop_id,
				'product_name_vi_VN'=>  $product->product_name_vi_VN,
				'product_name_en_US'=>  $product->product_name_en_US,
				'amount'			=>  $product->amount,	
				'original_price'	=> 	$product->original_price,
				'sale_price'		=> 	$product->sale_price,
				'images'			=>	$product->images,
				'note_used'			=>	$product->note_used,
				'product_unit'		=>	$product->product_unit,
				'product_unit_use'	=>	$product->product_unit_use,
				'volume_one'		=>	$product->volume_one,
				'volume_total'		=>	$product->volume_total,
				'block'				=>	$product->block,
			);
			
			if (isset($product->id) && $product->id != null) {
				$data['id'] = $product->id;
				$this->_conn->insert($this->_prefix . 'product', $data);
				return $product->id;				
			} else {
				$this->_conn->insert($this->_prefix . 'product', $data);
				return $this->_conn->lastInsertId($this->_prefix . 'product');
			}
	}
	
	public function update($product) 
	{
		$this->_conn->update($this->_prefix . 'product', 
							array(
									'id'				=>	$product->id,
									'shop_id'			=>  $product->shop_id,
									'product_name_vi_VN'=>  $product->product_name_vi_VN,
									'product_name_en_US'=>  $product->product_name_en_US,
									'amount'			=>  $product->amount,	
									'original_price'	=> 	$product->original_price,
									'sale_price'		=> 	$product->sale_price,
									'images'			=>	$product->images,
									'note_used'			=>	$product->note_used,
									'product_unit'		=>	$product->product_unit,
									'product_unit_use'	=>	$product->product_unit_use,
									'volume_one'		=>	$product->volume_one,
									'volume_total'		=>	$product->volume_total,
									'block'				=>	$product->block,
								), 
							array(
								'id = ?' => $product->id,
							));
	}
	
	public function delete($product) 
	{
		// delete all service_product
		$this->_conn->delete($this->_prefix . 'service_product', 
							array(
								'product_id = ?' => $product->id,
							));		
		// delete product					
		$this->_conn->delete($this->_prefix . 'product', 
							array(
								'id = ?' => $product->id,
							));
	}	
}